<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\WithdrawCash;

use App\Models\User;
use App\Models\Withdrawal;
use App\Models\WithdrawCashSetting;
use App\Service\BaseService;
use App\Service\Setting\BeanSettingService;
use App\Service\Statistics\StatisticsService;
use App\Service\User\BeanAccountService;
use App\Service\User\RewardAccountService;
use App\Service\User\WalletAccountService;

class WithdrawCashService extends BaseService
{
    public function withdrawCash(User $user, $amount) //提现到银行卡
    {
        $setting = $this->getWithdrawCashSetting(1);

        $bean = 0;
        if ($setting['bean'] > 0) {
            $beanPrice = StatisticsService::make()->getNowPrice();
            $bean = bcmul($setting['bean'], $amount, 2);
            $bean = bcdiv($bean, $beanPrice, 2);
            $msg = '提现到银行卡消耗KC通证' . $bean;
            $flag = BeanAccountService::make()
                ->consumption($user->id, $bean, $msg);

            if (! $flag) {
                return [
                    'error' => true,
                    'msg' => '提现到银行卡所需KC通证不足！',
                ];
            }
            BeanSettingService::make()->increaseTheAmountOfCombustion($bean, $msg);
        }
        $fee = bcmul($amount, $setting['fee'], 2);
        $nowAmount = bcsub($amount, $fee, 2);
        Withdrawal::query()->create([
            'amount' => $amount,
            'bean' => $bean,
            'fee' => $fee,
            'arrival_amount' => $nowAmount,
            'status' => 1,
            'applied_at' => now(),
            'user_bank_card_id' => $user->default_bank_card_id,
            'user_id' => $user->id,
        ]);
        $msg = sprintf('提现申请 金额:%s 手续费:%s 实际到账%s', $amount, $fee, $nowAmount);

        RewardAccountService::make()->consumption($user->id, $amount, 3, $msg);
        return [
            'error' => false,
            'msg' => $msg,
        ];
    }

    public function withdrawMoneyToTheWallet(User $user, $amount) //提现到钱包
    {
        $setting = $this->getWithdrawCashSetting(2);
        if ($setting['bean'] > 0) {
            $beanPrice = StatisticsService::make()->getNowPrice();
            $bean = bcmul($setting['bean'], $amount, 2);
            $bean = bcdiv($bean, $beanPrice, 2);

            $msg = '提现到钱包消耗KC通证' . $bean;
            $flag = BeanAccountService::make()
                ->consumption($user->id, $bean, '提现到钱包消耗KC通证' . $bean);
            if (! $flag) {
                return [
                    'error' => true,
                    'msg' => '提现到钱包所需KC通证不足！',
                ];
            }
            BeanSettingService::make()->increaseTheAmountOfCombustion($bean, $msg);
        }
        $fee = bcmul($amount, $setting['fee'], 2);
        $nowAmount = bcsub($amount, $fee, 2);
        $msg = sprintf('奖励余额转到账户 金额:%s 手续费:%s 实际到账%s', $amount, $fee, $nowAmount);
        RewardAccountService::make()->consumption($user->id, $amount, 4, sprintf('奖励余额转到账户 金额：%s', $amount));

        WalletAccountService::make()->recharge(
            $user->id,
            $nowAmount,
            $msg,
            false,
            5
        );
        return [
            'error' => false,
            'msg' => $msg,
        ];
    }

    public function withdrawMoneyToOthers(User $user, $amount, User $toUser) //提现到他人
    {
        $setting = $this->getWithdrawCashSetting(3);
        if ($setting['bean'] > 0) {
            $beanPrice = StatisticsService::make()->getNowPrice();
            $bean = bcmul($setting['bean'], $amount, 2);
            $bean = bcdiv($bean, $beanPrice, 2);
            $msg = '转账到他人消耗KC通证' . $bean;
            $flag = BeanAccountService::make()
                ->consumption($user->id, $bean, $msg);
            if (! $flag) {
                return [
                    'error' => true,
                    'msg' => '转账所需KC通证不足！',
                ];
            }
            BeanSettingService::make()->increaseTheAmountOfCombustion($bean, $msg);
        }
        $fee = bcmul($amount, $setting['fee'], 2);
        $nowAmount = bcsub($amount, $fee, 2);

        $msg = sprintf('来自用户:%s的转账 金额:%s 手续费:%s 实际到账%s', $user->telephone, $amount, $fee, $nowAmount);

        RewardAccountService::make()->consumption(
            $user->id,
            $amount,
            4,
            sprintf('奖励余额转到用户:%s 账户 金额:%s', $toUser->telephone, $amount)
        );

        RewardAccountService::make()->recharge(
            $toUser->id,
            $nowAmount,
            4,
            $msg,
        );
        return [
            'error' => false,
            'msg' => $msg,
        ];
    }

    public function getWithdrawCashSetting($type)
    {
        $withdrawCashSetting = WithdrawCashSetting::query()->first();
        switch ($type) {
            case 1:
                //提现
                return [
                    'fee' => $withdrawCashSetting->fee,
                    'bean' => $withdrawCashSetting->bean,
                ];
                break;
            case 2:
                //转账到钱包
                return [
                    'fee' => $withdrawCashSetting->transfer_wallet_fee,
                    'bean' => $withdrawCashSetting->transfer_wallet_bean,
                ];
                break;
            case 3:
                //转账他人
                return [
                    'fee' => $withdrawCashSetting->transfer_others_fee,
                    'bean' => $withdrawCashSetting->transfer_others_bean,
                ];
                break;
        }
    }
}
